package com.study.niuke;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Solution_NC61 {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * @param numbers int整型一维数组
     * @param target  int整型
     * @return int整型一维数组
     */
    public int[] twoSum(int[] numbers, int target) {
        // write code here
        Arrays.sort(numbers);
        int left = 0;
        int length = numbers.length;
        int right = length - 1;
        int sum;
        int[] res = {0, 0};
        while (left < right) {
            sum = numbers[right] + numbers[left];
            if (sum < target) {
                left++;
            } else if (sum > target) {
                right++;
            } else {
                res[0] = left + 1;
                res[1] = right + 1;
            }
        }
        return res;
    }

    public int[] twoSum1(int[] numbers, int target) {
        // write code here
        Map<Integer, Integer> map = new HashMap<>();
        int[] res = {0, 0};
        for (int i = 0; i < numbers.length; i++) {
            int temp = target - numbers[i];
            if (map.containsKey(temp)) {
                res[1] = i + 1;
                res[0] = map.get(temp) + 1;
            } else {
                map.put(numbers[i], i);
            }
        }
        return res;
    }

}
